package Test;

import com.lagou.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;
import utils.DBUtils;

import java.sql.*;
import java.util.List;
import java.util.Scanner;

/**
 * @author StarryHorizon
 * @create 2022-01-17 11:27
 */

/**
 * 编写一个测试类，在该测试类中使用PrepareStatement 根据name和gender进行数据查询，并将结果打印在控制台。
 */
public class TestMyTask {

    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //1.获取连接对象
            con = DBUtils.getConnection();

            //2.获取prepareStatement对象
            String sql = "select * from employee where name = ? and gender = ?";
            ps = con.prepareStatement(sql);

            //3.获取用户输入的姓名和性别
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要查询人物的姓名");
            String name = sc.next();

            System.out.println("请输入要查询人物的性别");
            String gender = sc.next();

            //4.设置参数
            ps.setString(1, name);
            ps.setString(2, gender);

            //5.执行查询
            rs = ps.executeQuery();

            //6.处理结果集
            while (rs.next()) {
                int id = rs.getInt("id");
                String name1 = rs.getString("name");
                String gender1 = rs.getString("gender");
                double salary = rs.getDouble("salary");
                double bonus = rs.getDouble("bonus");
                Date join_date = rs.getDate("join_date");

                System.out.println("编号：" + id + " 姓名：" + name1 + " 性别：" + gender1 + " 薪资：" + salary +
                " 奖金：" + bonus + " 入职时间：" + join_date );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //7.关闭流
            DBUtils.close(con, ps, rs);
        }




    }
}
